#include "../include/client.h"
client::client()
{
    return;
}
client::~client()
{

}
	int client::init()
	{
	    #ifdef _WIN32
            WSADATA wsa;
            if(WSAStartup(0x0202,&wsa) == -1)
            {
                cout<<"Wnsock init failed !!"<<endl;
                exit(-1);
            }
            else
            {

            #endif

                    getparametter();
                    int status = createsocket();
                    if(status == 0)
                    {

                        return 0;
                    }
                    else
                    {

                        return -1;
                    }
            #ifdef _WIN32
            }
            #endif

	}
	//lay para can thiet
	void client::getparametter()
	{
		cout<<endl<<"Server ip : ";
		cin>>this->sv_addr;
		this->port = 9999;
		server.sin_family = AF_INET;
		server.sin_addr.s_addr = inet_addr(sv_addr);
		server.sin_port = htons(port);

	}
	int client::createsocket()
	{
	    cl = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
		if(cl == -1)
		{

			cout<<"Create socket failed: "<<endl;
			exit(2);
			return -1;
		}
		else
		{
		    cout<<"Create socket successfull"<<endl;
			return 0;
		}
	}
	void client::clientconnect()
	{
		int con = connect(cl,(sockaddr *)&server,sizeof(server));
		if(con == -1)
		{
			cout<<"Connected server failed :"<<endl;
			finish();

		}
		else
		{
			cout<<"Connected server successfull "<<endl;

		}
	}

	int client::clientsend()
	{
       // sent.printmessage();
		int size = send(cl,(char *)&sent,sizeof(sent),0);
        //cout<<"Client sent : "<<size<<endl;
		return size;

	}
	void client::clientrecv()
	{

		recv(cl,(char *)&buff,sizeof(buff),0);
    }
	void client::playgame()
	{
			cout<<"---------------------------------------------------------"<<endl;
			cout<<"\t\tGame start\t\t"<<endl;
			buff.setvalue(-1,-1,-1," ");
			int size=0;
			bool flag = false;
			while(buff.gettype() != 0)
			{

				clientrecv();
				if(buff.gettype() == 3)
				{
					size = buff.getx();
					table = new map(buff.getx());
				}
				else if(buff.gettype() == 4 || buff.gettype() == -4)
				{
					table->draw();
					int a,b;
					again:cout<<"Please,choose a position on map : "<<endl;
					cout<<"Row : ";cin>>a;
					cout<<"Col  :  ";cin>>b;
					if(a<=0||a >size||b<=0||b>size)
					{
						goto again;
					}else
					{
						sent.setvalue(4,a,b,"POSITION");
						clientsend();
					}

				}
				//gui cho tat ca
				else if(buff.gettype() == 10)
				{
					string d;
					d = buff.getmessage();
					table->setvalue(d[0],buff.getx(),buff.gety());
					table->draw();
				}
				else if(buff.gettype() == 9)
				{
					if(flag == false)
					{
						cout<<"--------------------Result -----------------------"<<endl;
						cout<<buff.getmessage()<<"  :  "<<buff.getx()<<" points"<<endl;
					}
					else
					{
						cout<<buff.getmessage()<<"  :  "<<buff.getx()<<" points"<<endl;
					}
					flag = true;
				}


			};
			cout<<"Game finished!!!"<<endl;
			//type = 0 la thoat hoac ket thuc chuong trinh
			//1 = login
			// 2 login thanh cong
			//3 la play game,chon hang cot,-3 la chon lai do cot do da chon roi


	}
	void client::login()
	{
	    while(1)
			{
				clientrecv();
				if(buff.gettype() == 2)
				{
					cout<<"Register user sucess, your nicknmae : "<<user<<endl;
					break;
				}
				else
				{
					cout<<"Input user name :";
					cin>>user;
					sent.setvalue(1,-1,-1,user);
					clientsend();

				}

			}
	}
	void client::run()
	{
		if(init() == 0)
		{
			clientconnect();
			login();
			playgame();

		}
		finish();
	}
	void client::finish()
	{
		//shutdown(cl,SD_BOTH);
		#ifdef _WIN32
            closesocket(cl);
            WSACleanup();
        #endif
        #ifdef linux
            close(cl);
		#endif
		delete table;
		cin.get();

	}


